package com.lock.subscribe.service;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import com.lock.subscribe.constant.ClientDataEnum;
import com.lock.subscribe.constant.ClientOpenTypeEnum;
import com.lock.subscribe.constant.CmdDataKey;
import com.lock.subscribe.constant.CmdDelDataKey;
import com.lock.subscribe.constant.WebEventKey;
import com.lock.subscribe.msg.MessageConversionAdapter;
import com.lock.subscribe.msg.MessageMapAllParse;
import com.lock.subscribe.msg.MessageMapParse;
import com.lock.subscribe.offlinepwd.OfflinePassword;
import com.lock.subscribe.offlinepwd.OfflinePasswordSo;
import com.lock.subscribe.offlinepwd.OfflinePasswordSo2;
import com.lock.subscribe.util.Base64Util;
import com.lock.subscribe.util.ByteTools;
import com.lock.subscribe.util.DateUtil;
import com.lock.subscribe.util.HexUtils;
import com.lock.subscribe.util.QrCodeUtil;

public class LockService {
	private static Logger logger = LoggerFactory.getLogger(LockService.class);
	private final static Integer openTypelength = 1;
	private final static Integer pitNumLength = 1;
	private final static Integer multiple = 2;
	private final static String syncTime = "rep_syncTime";
	private final static String errorOpenLock = "rep_errorOpenLock";
	private final static String alarm = "rep_alarm";
	private final static Integer packageFm226Size = 2048;
	private final static String servicetype = "0024";
	private final static String sendMessage = "E8";

	/**
	 * 远程开锁上报数据
	 */
	private static JSONObject handlerGeneralMqttMessage(String hexData) {

		JSONObject postParam = new JSONObject();
		try {
			if (Base64Util.isBase64(hexData)) {
				hexData = ByteTools.bytesToString(Base64Util.decode(hexData));
			}
			String deviceId = MessageConversionAdapter.getDeviceId(hexData);
			String data = MessageConversionAdapter.getData(hexData);
			String stamp = MessageConversionAdapter.getTimestamp(hexData);
			long st = Long.valueOf(stamp, 16) - 60 * 60 * 8;
			Integer starus = MessageConversionAdapter.getSuccessFlag(data) == 0 ? 1 : 0;// 0成功 其他失败
			postParam.put("deviceid", deviceId);
			postParam.put("result", starus);
			postParam.put("reportTime", DateUtil.timeStamp2Date(st + "", null));
			postParam.put("msgType", ClientOpenTypeEnum.OPEN.getData());
			postParam.put("eventType", null);
			postParam.put("password", null);
			postParam.put("alarmtype", null);// 3为密码多次试错报警，4为感应卡多次试错报警，5为指纹多次试错报警
			postParam.put("electric", null);
			postParam.put("simNo", null);
			postParam.put("signalInfo", null);
			postParam.put("syncTime", null);
			postParam.put("pitNum", null);
			postParam.put("stamp", stamp);
		} catch (Exception e) {
			logger.error("handlerGeneralMqttMessage :" + hexData);
		}
		return postParam;
	}

	/**
	 * 处理添加/删除开锁权限回复数据
	 * 
	 * @param hexData
	 * @return
	 */
	private static JSONObject handlerPasswordMessage(String hexData) {
		JSONObject postParam = new JSONObject();
		try {
			if (Base64Util.isBase64(hexData)) {
				hexData = ByteTools.bytesToString(Base64Util.decode(hexData));
			}
			String deviceId = MessageConversionAdapter.getDeviceId(hexData);
			String stamp = MessageConversionAdapter.getTimestamp(hexData);
			String data = MessageConversionAdapter.getData(hexData);
			String type = MessageConversionAdapter.getMiniType(data);
			Integer pitNum = MessageConversionAdapter.getpitNum(data);
			Integer result = MessageConversionAdapter.getSuccessFlag(data);
			postParam.put("deviceId", deviceId);
			postParam.put("alarmtype", null);// 3为密码多次试错报警，4为感应卡多次试错报警，5为指纹多次试错报警
			postParam.put("electric", null);
			postParam.put("simNo", null);
			postParam.put("signalInfo", null);
			postParam.put("syncTime", null);
			postParam.put("eventType", null);
			postParam.put("msgType", type.toUpperCase());
			postParam.put("stamp", stamp);
			long st = Long.valueOf(stamp, 16) - 60 * 60 * 8;
			postParam.put("reportTime", DateUtil.timeStamp2Date(st + "", null));
			if (result == 80) {
				postParam.put("result", result);
			} else {
				postParam.put("result", result == 0 ? 1 : 0);
			}

			postParam.put("passwordId", pitNum);
			if (ClientOpenTypeEnum.CODE.getData().equals(type)) {
				data = MessageConversionAdapter.getCodeData(hexData);
				postParam.put("result", MessageConversionAdapter.getCodeIdDelFlag(data) == 0 ? 1 : 0);
				postParam.put("passwordId", MessageConversionAdapter.getCodeIdData(data));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return postParam;
	}
	/**
	 * 处理催缴/停止催缴/冻结/解冻回复数据
	 * 
	 * @param hexData
	 * @return
	 */
	private static JSONObject handlerSetTimeMessage(String hexData) {
		JSONObject postParam = new JSONObject();
		try {
			if (Base64Util.isBase64(hexData)) {
				hexData = ByteTools.bytesToString(Base64Util.decode(hexData));
			}
			String deviceId = MessageConversionAdapter.getDeviceId(hexData);
			String stamp = MessageConversionAdapter.getTimestamp(hexData);
			String data = MessageConversionAdapter.getSetTimeData(hexData);
			String type = MessageConversionAdapter.getMiniType(data);
			boolean result = MessageConversionAdapter.getResult(data);
			postParam.put("deviceId", deviceId);
			postParam.put("alarmtype", null);// 3为密码多次试错报警，4为感应卡多次试错报警，5为指纹多次试错报警
			postParam.put("electric", null);
			postParam.put("simNo", null);
			postParam.put("signalInfo", null);
			postParam.put("syncTime", null);
			postParam.put("eventType", null);
			postParam.put("msgType", type.toUpperCase());
			postParam.put("stamp", stamp);
			long st = Long.valueOf(stamp, 16) - 60 * 60 * 8;
			postParam.put("reportTime", DateUtil.timeStamp2Date(st + "", null));
			postParam.put("result", result == true ? 1 : 0);
			postParam.put("passwordId", null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return postParam;
	}
	/**
	 ** 开锁
	 * 
	 * @param mac
	 */
	public static String getOpenHexData(String mac, String bleName, long stamp) {
		String sendData = "";
		String deviceId = getDeviceId(mac);
		try {
			String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
			String stampSend = HexUtils.toHexL(stamp + 60 * 60 * 8 + "", 8);
			String status = ClientOpenTypeEnum.OPEN.getData();
			String data = MessageConversionAdapter.getOpenLockData(StringUtils.leftPad(status, 2, "0"), stampSend);
			sendData = MessageConversionAdapter.getSendGeneralData(deviceId, data.length() / 2, data,
					ClientDataEnum.GENERAL.getData(), ClientOpenTypeEnum.OPEN.getName(), random, stampSend,
					getLockType(bleName));
		} catch (Exception e) {
			logger.error("MqttCallback sendOpenMessage:" + sendData);
		}
		return sendData;
	}

	/**
	 ** 设置心跳
	 * 
	 * @param mac
	 */
	public static String setPingHexData(String mac, String bleName, long stamp) {
		String sendData = "";
		String deviceId = getDeviceId(mac);
		try {
			String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
			String stampSend = HexUtils.toHexL(stamp + 60 * 60 * 8 + "", 8);
			String status = ClientOpenTypeEnum.OPEN.getData();
			String data = MessageConversionAdapter.getOpenLockData(StringUtils.leftPad(status, 2, "0"), stampSend);
			sendData = MessageConversionAdapter.getSendGeneralData(deviceId, data.length() / 2, data,
					ClientDataEnum.GENERAL.getData(), ClientOpenTypeEnum.PING.getName(), random, stampSend,
					getLockType(bleName));
		} catch (Exception e) {
			logger.error("MqttCallback sendOpenMessage:" + sendData);
		}
		return sendData;
	}

	/**
	 * @param mac        蓝牙mac地址
	 * @param startTime  密钥有效使用开始时间
	 * @param endTime    密钥有效使用结束时间
	 * @param password   密钥数据
	 * @param passwordId 密码ID(坑号)
	 * @param type       1:指纹 2:密码 3:卡片 4:人脸
	 */
	public static String getAddHexData(String mac, String startTime, String endTime, String password,
			Integer passwordId, Integer type, String bleName, long stamp) {
		String sendMsg = "";
		String sendData = "";
		String deviceId = getDeviceId(mac);
		try {

			if (type == 1) {
				String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
				String stampSend = HexUtils.toHexL(stamp + "", 8);
				sendMsg = MessageConversionAdapter.getSendDataFinger(passwordId, password, startTime, endTime);
				sendData = MessageConversionAdapter.getSendAddFingerData(deviceId, sendMsg.length() / 2, sendMsg,
						ClientOpenTypeEnum.FINGER.getName(), ClientDataEnum.FINGER.getData(), random, stampSend,
						getLockType(bleName));
				return sendData;

			} else if (type == 2) {
				String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
				String stampSend = HexUtils.toHexL(stamp + "", 8);
				sendMsg = sendMsg + MessageConversionAdapter.getSendDataPwd(password, passwordId,
						ClientOpenTypeEnum.PASSWORD.getName(), startTime, endTime);
				sendData = MessageConversionAdapter.getSendAddOpenTypeData(deviceId, sendMsg.length() / 2, sendMsg,
						ClientDataEnum.PASSWORD.getData(), random, stampSend, getLockType(bleName));
				return sendData;
			} else if (type == 3) {
				String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
				String stampSend = HexUtils.toHexL(stamp + "", 8);
				sendMsg = sendMsg + MessageConversionAdapter.getSendDataCard(password, passwordId,
						ClientOpenTypeEnum.CARD.getName(), startTime, endTime);
				sendData = MessageConversionAdapter.getSendAddOpenTypeData(deviceId, sendMsg.length() / 2, sendMsg,
						ClientDataEnum.CARD.getData(), random, stampSend, getLockType(bleName));
				return sendData;
			} else if (type == 4) {
				String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
				String stampSend = HexUtils.toHexL(stamp + "", 8);
				sendMsg = MessageConversionAdapter.getSendDataInfo(passwordId, password, startTime, endTime);
				sendData = MessageConversionAdapter.getSendFacesData(deviceId, sendMsg.length() / 2, sendMsg,
						ClientOpenTypeEnum.FACESCC.getName(), ClientDataEnum.FACES.getData(), random, stampSend,
						getLockType(bleName));
				return sendData;
			} else if (type == 5) {
				String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
				String stampSend = HexUtils.toHexL(stamp + "", 8);
				sendMsg = MessageConversionAdapter.getSendFm223Info(passwordId, password, startTime, endTime);
				sendData = MessageConversionAdapter.getSendFacesData(deviceId, sendMsg.length() / 2, sendMsg,
						ClientDataEnum.CALLBACKFM226.getData(), ClientOpenTypeEnum.FM226FEATURE.getData(), random,
						stampSend, getLockType(bleName));
				return sendData;
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("MqttCallback sendAddOpenTypeMessage:" + sendData);
		}
		return sendData;
	}

	/**
	 * @param mac        蓝牙mac地址
	 * @param startTime  密钥有效使用开始时间
	 * @param endTime    密钥有效使用结束时间
	 * @param password   密钥数据
	 * @param passwordId 密码ID(坑号)
	 */
	public static JSONObject getAddImageHexData(String mac, String startTime, String endTime, String password,
			Integer passwordId, String bleName, long stamp) {
		JSONObject sendData = new JSONObject();
		String sendMsg = "";
		String hexImageData = "";
		String deviceId = getDeviceId(mac);
		try {

			String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
			String stampSend = HexUtils.toHexL(stamp + "", 8);
			sendMsg = MessageConversionAdapter.getSendFm223Info(passwordId, password, startTime, endTime);
			String replyData = MessageConversionAdapter.getSendFacesData(deviceId, sendMsg.length() / 2, sendMsg,
					ClientDataEnum.CALLBACKFM226.getData(), ClientOpenTypeEnum.FM226FEATURE.getData(), random,
					stampSend, getLockType(bleName));
			sendData.put("replyData", replyData);
			hexImageData = MessageConversionAdapter.spitFm226ImageData(password, 0, packageFm226Size);
			String firstData = MessageConversionAdapter.getSendFm226FacesData(deviceId, packageFm226Size, hexImageData,
					servicetype, sendMessage, random, stampSend, password.length(), 0);
			sendData.put("firstData", firstData);
			hexImageData = MessageConversionAdapter.spitFm226ImageData(password, packageFm226Size, password.length());
			String secondData = MessageConversionAdapter.getSendFm226FacesData(deviceId, hexImageData.length(),
					hexImageData, servicetype, sendMessage, random, stampSend, password.length(), packageFm226Size / 2);
			sendData.put("secondData", secondData);
			sendData.put("passwordId", passwordId);

			return sendData;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("MqttCallback sendAddOpenTypeMessage:" + sendData);
		}
		return sendData;
	}

	/**
	 * 获取二维码数据
	 * 
	 * @param mac       蓝牙mac地址
	 * @param startTime 密钥有效使用开始时间
	 * @param endTime   密钥有效使用结束时间
	 * @param baseCode  小程序绑定的原始密码
	 */
	public static String getCodeData(String mac, String startTime, String endTime, String baseCode, long stamp) {
		return QrCodeUtil.getDeviceQrData(mac, baseCode, stamp, startTime, endTime);

	}

	/**
	 * @param mac        蓝牙mac地址
	 * @param startTime  密钥有效使用开始时间
	 * @param endTime    密钥有效使用结束时间
	 * @param password   密钥数据
	 * @param passwordId 密码ID(坑号)
	 * @param type       1:指纹 4:人脸
	 */
	public static String getAddByteData(String mac, String startTime, String endTime, byte[] password,
			Integer passwordId, Integer type, String bleName, long stamp) {
		String sendMsg = "";
		String sendData = "";
		String deviceId = getDeviceId(mac);
		if (password == null || password.length == 0)
			return null;
		try {
			if (type == 1) {
				String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
				String stampSend = HexUtils.toHexL(stamp + "", 8);
				String result = HexUtils.bytesToHexString(password, password.length);
				sendMsg = MessageConversionAdapter.getSendDataFinger(passwordId, result, startTime, endTime);
				sendData = MessageConversionAdapter.getSendAddFingerData(deviceId, sendMsg.length() / 2, sendMsg,
						ClientOpenTypeEnum.FINGER.getName(), ClientDataEnum.FINGER.getData(), random, stampSend,
						getLockType(bleName));
				return sendData;
			} else if (type == 4) {
				String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
				String stampSend = HexUtils.toHexL(stamp + "", 8);
				String hexImageData = MessageConversionAdapter.spitImageDataRep(password, 0, password.length,
						passwordId);
				sendMsg = MessageConversionAdapter.getSendDataFeature(passwordId, hexImageData, startTime, endTime);
				sendData = MessageConversionAdapter.getSendGeneralData04(deviceId, sendMsg.length() / 2, sendMsg,
						ClientDataEnum.FEATURE.getData(), ClientOpenTypeEnum.FEATURE.getName(), random, stampSend,
						getLockType(bleName));
				return sendData;

			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error("MqttCallback sendAddOpenTypeMessage:" + sendData);
		}
		return sendData;
	}

	/**
	 * 发送删除开门方式
	 * 
	 * @param mac
	 * @param passwordId
	 * @param type       1:指纹 2:密码 3:卡片 4:人脸 5:离线密码
	 * @return
	 */
	public static String getDelHexData(String mac, Integer passwordId, Integer type, String bleName, long stamp) {
		String sendData = "";
		String deviceId = getDeviceId(mac);
		try {
			if (type == 1) {
				String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
				String stampSend = HexUtils.toHexL(stamp + "", 8);
				sendData = MessageConversionAdapter.getSendDelData(deviceId, passwordId,
						ClientDataEnum.GENERAL.getData(), ClientOpenTypeEnum.DELFINGER.getName(), random, stampSend,
						getLockType(bleName));
				return sendData;
			}
			if (type == 2) {
				String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
				String stampSend = HexUtils.toHexL(stamp + "", 8);
				sendData = MessageConversionAdapter.getSendDelData(deviceId, passwordId,
						ClientDataEnum.PASSWORD.getData(), ClientOpenTypeEnum.DELPWD.getName(), random, stampSend,
						getLockType(bleName));
				return sendData;
			}
			if (type == 3) {
				String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
				String stampSend = HexUtils.toHexL(stamp + "", 8);
				sendData = MessageConversionAdapter.getSendDelData(deviceId, passwordId, ClientDataEnum.CARD.getData(),
						ClientOpenTypeEnum.DELCARD.getName(), random, stampSend, getLockType(bleName));
				return sendData;
			}

			if (type == 4) {
				String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
				String stampSend = HexUtils.toHexL(stamp + "", 8);
				sendData = MessageConversionAdapter.getSendDelData(deviceId, passwordId,
						ClientDataEnum.DELIMAGE.getData(), ClientOpenTypeEnum.DELFACES.getName(), random, stampSend,
						getLockType(bleName));
				return sendData;
			}
			if (type == 5) {// 删除离线密码
				String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
				String stampSend = HexUtils.toHexL(stamp + "", 8);
				String datas = MessageConversionAdapter.getOfflinePwd(ClientOpenTypeEnum.OFFLINEPWD.getData(),
						passwordId);
				sendData = MessageConversionAdapter.getOfflineSendData(deviceId, datas,
						ClientDataEnum.OFFLINEPWD.getData(), ClientDataEnum.GENERAL.getData(), random, stampSend,
						getLockType(bleName));
				return sendData;
			}
		} catch (Exception e) {
			logger.error("MqttCallback sendAddOpenTypeMessage:" + sendData);
		}
		return sendData;
	}

	/**
	 * 发送删除二维码数据
	 * 
	 * @param mac
	 * @param bleName
	 * @param passwordId
	 * @return
	 */
	public static String getDelCodeIdData(String mac, String passwordId, String bleName) {
		String sendData = "";
		String deviceId = getDeviceId(mac);
		try {
			String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
			sendData = MessageConversionAdapter.getSendDelCodeData(deviceId, passwordId,
					ClientDataEnum.GENERAL.getData(), ClientOpenTypeEnum.CODE.getName(), random, passwordId,
					getLockType(bleName));
			return sendData;
		} catch (Exception e) {
			logger.error("MqttCallback sendAddOpenTypeMessage:" + sendData);
		}
		return sendData;
	}

	public static JSONObject handlerReportData(String hexData) {
		if (Base64Util.isBase64(hexData)) {
			hexData = ByteTools.bytesToString(Base64Util.decode(hexData));
		}
		String data = MessageConversionAdapter.getReportData(hexData);
		if (data.equals("")) {
			return null;
		}
		String type = MessageConversionAdapter.getMiniType(data).toUpperCase();
		if (data.startsWith("70") || data.startsWith("71") || data.startsWith("73")) {
			return handlerReportMessage(hexData);
		} else if (type.equals(ClientOpenTypeEnum.PASSWORD.getData())
				|| type.equals(ClientOpenTypeEnum.DELPWD.getData()) || type.equals(ClientOpenTypeEnum.CARD.getData())
				|| type.equals(ClientOpenTypeEnum.DELCARD.getData()) || type.equals(ClientOpenTypeEnum.FINGER.getData())
				|| type.equals(ClientOpenTypeEnum.DELFINGER.getData())
				|| type.equals(ClientOpenTypeEnum.FACESCC.getData())
				|| type.equals(ClientOpenTypeEnum.DELFACES.getData())
				|| type.equals(ClientOpenTypeEnum.DELFM226FEATURE.getData())
				|| type.equals(ClientOpenTypeEnum.DELFM226FEATURE.getName())
				|| type.equals(ClientOpenTypeEnum.CODE.getData())) {
			return handlerPasswordMessage(hexData);
		} else if (type.equals(ClientOpenTypeEnum.SYNCTIME.getName())
				|| type.equals(ClientOpenTypeEnum.OFFLINEPWD.getName())) {
			return handlerOfflineMqttMessage(hexData);
		} else if (type.equals(ClientOpenTypeEnum.OPEN.getData())) {
			return handlerGeneralMqttMessage(hexData);
		}else if(type.equals(ClientOpenTypeEnum.SETTIME.getName())) {
			return handlerSetTimeMessage(hexData);
		}
		return new JSONObject();
	}

	private static JSONObject handlerReportMessage(String hexData) {
		JSONObject postParam = new JSONObject();
		try {
			String cmdData = "";
			Integer pitNum = null;
			Integer electric = 0;
			Integer signalInfo = 0;
			Integer openFlage = null;
			String sim = "";
			String errorLog = "";
			Integer openType = null;
			String msgType = "00";
			String version = "";
			if (hexData.startsWith("") && Base64Util.isBase64(hexData)) {
				hexData = ByteTools.bytesToString(Base64Util.decode(hexData));
			}
			String deviceId = MessageConversionAdapter.getDeviceId(hexData);
			String stamp = MessageConversionAdapter.getTimestamp(hexData);
			String data = MessageConversionAdapter.getReportData(hexData);

			Map<String, Object> datamap = MessageMapParse.parseMessage(data.length(), data);
			if (datamap.get("rep_electric") != null) {
				electric = Integer.valueOf(datamap.get("rep_electric") + "");
			}
			if (datamap.get("rep_signalInfo") != null) {
				signalInfo = Integer.valueOf(datamap.get("rep_signalInfo") + "");
			}
			if (datamap.get("rep_version") != null) {
				version = datamap.get("rep_version").toString();
			}
			if (datamap.get("rep_openType") != null) {// MMXXSSSSSSSS开锁三元组式( MM为开锁方式
				// 02//超级密码、03//机械开锁、04//离线密码、05//离线卡、06//限时指纹、07//限时密码、08//限时感应卡、09//限时人脸))，XX为开锁坑位，SSSSSSSS为开锁信息式(密码或卡号等数据))
				String revData = datamap.get("rep_openType").toString();
				String type = revData.substring(0, openTypelength * multiple);
				if (type.toUpperCase().equals("0A")) {
					openType = 11;
				} else if (type.toUpperCase().equals("FF")) {
					openType = 4;
				} else {
					openType = Integer.valueOf(revData.substring(0, openTypelength * multiple));
				}

				if (openType == 6 || openType == 7 || openType == 8 || openType == 9) {
					pitNum = Integer.valueOf(
							revData.substring((openTypelength) * multiple, (openTypelength + pitNumLength) * multiple),
							16);
				}
				if (openType == 7 || openType == 4) {
					cmdData = Integer.valueOf(
							revData.substring((openTypelength + pitNumLength) * multiple, revData.length()), 16) + "";
				}
				if (openType == 2) {
					cmdData = Integer.valueOf(
							revData.substring((openTypelength + pitNumLength) * multiple, revData.length()), 16) + "";
				}
				if (openType == 11) {
					cmdData = revData.substring((openTypelength + pitNumLength) * multiple, revData.length());
				}
				msgType = StringUtils.leftPad(openType.toString(), 2, "0");
			}
			if (datamap.get("rep_errorLog") != null) {// 错误日志
				errorLog = datamap.get("rep_errorLog").toString();// 03为密码多次试错报警，04为感应卡多次试错报警，05为指纹多次试错报警
			}
			if (datamap.get("rep_sim") != null) {
				sim = datamap.get("rep_sim").toString();

			}
			if (datamap.get("rep_openFlage") != null) {
				openFlage = Integer.valueOf(datamap.get("rep_openFlage").toString());
			}
			postParam.put("stamp", stamp);
			postParam.put("openStatus", openFlage);
			postParam.put("password", cmdData);
			postParam.put("deviceId", deviceId);
			postParam.put("alarmtype", errorLog == "" ? null : Integer.valueOf(errorLog));// 3为密码多次试错报警，4为感应卡多次试错报警，5为指纹多次试错报警
			postParam.put("electric", electric);
			postParam.put("eventType", openType);
			postParam.put("version", version);
			postParam.put("msgType", msgType.toUpperCase());
			postParam.put("simNo", sim);
			long st = Long.valueOf(stamp, 16) - 60 * 60 * 8;
			postParam.put("reportTime", DateUtil.timeStamp2Date(st + "", null));
			postParam.put("signalInfo", signalInfo);
			postParam.put("result", 1);
			postParam.put("syncTime", null);
			postParam.put("passwordId", pitNum);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("MqttCallback handlerReportMqttMessage:" + hexData);
		}
		return postParam;
	}

	/**
	 ** 同步时间
	 * 
	 * @param deviceId
	 */
	public static JSONObject sendSyncClock(String mac, String bleName) {
		JSONObject postParam = new JSONObject();
		String sendData = "";
		String deviceId = getDeviceId(mac);
		long stamplong = 0;
		try {
			String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
			String stamp = HexUtils.toHexL(stamplong + "", 8);
			String data = MessageConversionAdapter.getOfflineData(ClientOpenTypeEnum.SYNCTCLOCK.getData(), stamp);
			sendData = MessageConversionAdapter.getOfflineSendData(deviceId, data, ClientDataEnum.OFFLINEPWD.getData(),
					ClientDataEnum.GENERAL.getData(), random, stamp, getLockType(bleName));
			postParam.put("data", sendData);
			postParam.put("stamp", stamp);
			postParam.put("result", 1);
			return postParam;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("MqttCallback sendSyncTime:" + sendData);
		}
		postParam.put("data", sendData);
		postParam.put("stamp", null);
		postParam.put("result", 0);
		return postParam;
	}

	/**
	 ** 同步离线密码时间
	 * 
	 * @param deviceId
	 */
	public static JSONObject sendSyncTime(String mac, String bleName) {
		JSONObject postParam = new JSONObject();
		String sendData = "";
		String deviceId = getDeviceId(mac);
		try {
			long stamplong = DateUtil.getNowSub(3) - DateUtil.getNowSub(3) % 4115;
			String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
			String stamp = HexUtils.toHexL(stamplong + "", 8);
			String data = MessageConversionAdapter.getOfflineData(ClientOpenTypeEnum.SYNCTIME.getData(), stamp);
			sendData = MessageConversionAdapter.getOfflineSendData(deviceId, data, ClientDataEnum.OFFLINEPWD.getData(),
					ClientDataEnum.GENERAL.getData(), random, stamp, getLockType(bleName));
			postParam.put("data", sendData);
			postParam.put("stamp", stamp);
			postParam.put("result", 1);
			return postParam;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("MqttCallback sendSyncTime:" + sendData);
		}
		postParam.put("data", sendData);
		postParam.put("stamp", null);
		postParam.put("result", 0);
		return postParam;
	}

	private static JSONObject handlerOfflineMqttMessage(String hexData) {
		JSONObject postParam = new JSONObject();
		try {
			if (Base64Util.isBase64(hexData)) {
				hexData = ByteTools.bytesToString(Base64Util.decode(hexData));
			}
			String deviceId = MessageConversionAdapter.getDeviceId(hexData);
			String stamp = MessageConversionAdapter.getTimestamp(hexData);
			String data = MessageConversionAdapter.getData(hexData);
			String type = MessageConversionAdapter.getMiniType(data);
			if (type.toUpperCase().equals(ClientOpenTypeEnum.SYNCTIME.getName())) {
				postParam.put("syncTime", Integer.parseInt(stamp, 16));
			} else {
				postParam.put("syncTime", null);
			}
			postParam.put("deviceId", deviceId);
			postParam.put("eventType", null);
			postParam.put("stamp", stamp);
			postParam.put("msgType", type.toUpperCase());
			postParam.put("reportTime", DateUtil.getNowTimeStr());
			postParam.put("password", null);
			postParam.put("alarmtype", null);// 3为密码多次试错报警，4为感应卡多次试错报警，5为指纹多次试错报警
			postParam.put("electric", null);
			postParam.put("simNo", null);
			postParam.put("signalInfo", null);
			postParam.put("passwordId", null);
			postParam.put("status", MessageConversionAdapter.getSuccessFlag(data) == 0 ? 1 : 0);
		} catch (Exception e) {
		}
		return postParam;
	}

	/**
	 * 
	 * @param sysType   1:windowd 2:linux
	 * @param startTime
	 * @param endTime
	 * @param password
	 * @param stamp
	 * @return
	 */
	public static JSONObject getOfflinePassword(String mac, Integer sysType, String startTime, String endTime,
			long stamp) {
		Long password = null;
		JSONObject postParam = new JSONObject();
		if (DateUtil.getType(startTime, endTime) == 1) {
			if (sysType == 1) {
				password = OfflinePassword.fiveMinPwd(stamp, mac,
						DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8);
			} else {
				password = OfflinePasswordSo.fiveMinPwd(stamp, mac,
						DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8);
			}
		} else if (DateUtil.getType(startTime, endTime) == 2) {
			if (sysType == 1) {
				password = OfflinePassword.threeDayPwd(stamp, mac,
						DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8,
						DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8);
			} else {
				password = OfflinePasswordSo.threeDayPwd(stamp, mac,
						DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8,
						DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8);
			}
		} else if (DateUtil.getType(startTime, endTime) == 3) {
			if (sysType == 1) {
				password = OfflinePassword.ninetythreeDayPwd(stamp, mac,
						DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8,
						DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8);
			} else {
				password = OfflinePasswordSo.ninetythreeDayPwd(stamp, mac,
						DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8,
						DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8);
			}
		} else {
			if (sysType == 1) {
				password = OfflinePassword.oneyearPwd(stamp, mac, DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8,
						DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8);
			} else {
				password = OfflinePasswordSo.oneyearPwd(stamp, mac,
						DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8,
						DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8);
			}
		}
		String deviceId = getDeviceId(mac);
		postParam.put("deviceId", deviceId);
		postParam.put("eventType", null);
		postParam.put("stamp", stamp);
		postParam.put("msgType", ClientOpenTypeEnum.OFFLINEPWD.getData());
		postParam.put("reportTime", DateUtil.getNowTimeStr());
		postParam.put("password", password);
		postParam.put("alarmtype", null);
		postParam.put("electric", null);
		postParam.put("simNo", null);
		postParam.put("signalInfo", null);
		postParam.put("passwordId", null);
		return postParam;
	}

	/**
	 * 
	 * @param mac
	 * @param type     0:一次密码1:小时密码 2:天密码
	 * @param sysType   1:windowd 2:linux
	 * @param startTime
	 * @param endTime
	 * @param stamp
	 * @return
	 */
	public static JSONObject getNewOfflinePassword(String mac, Integer type, Integer sysType, String startTime,
			String endTime, long stamp) {
		Long password = null;
		JSONObject postParam = new JSONObject();
		long startTiem = DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8;
		long endTiem = DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8;
		if (type == 0) {
			password = OfflinePasswordSo2.onePwd(0, stamp, mac, startTiem, endTiem);
		} else if (type == 1) {
			password = OfflinePasswordSo2.hourPwd(0, stamp, mac, startTiem, endTiem);
		} else if (type == 2) {
			password = OfflinePasswordSo2.dayPwd(1, stamp, mac, startTiem, endTiem);
		}
		String deviceId = getDeviceId(mac);
		postParam.put("deviceId", deviceId);
		postParam.put("eventType", null);
		postParam.put("stamp", stamp);
		postParam.put("msgType", ClientOpenTypeEnum.OFFLINEPWD.getData());
		postParam.put("reportTime", DateUtil.getNowTimeStr());
		postParam.put("password", password);
		postParam.put("alarmtype", null);
		postParam.put("electric", null);
		postParam.put("simNo", null);
		postParam.put("signalInfo", null);
		postParam.put("passwordId", null);
		return postParam;
	}

	/**
	 * 
	 * @param mac
	 * @param bleName
	 * @param type      0:催缴1:停止催缴2:冻结3:解冻
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static String getSettingTimeMessage(String mac, String bleName, Integer type, String startTime,
			String endTime, long stamp) {
		String deviceId = getDeviceId(mac);
		String freezeTime = "";
		try {
			if (type == 0) {// 催缴
				startTime = HexUtils.toHexL(DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8 + "", 8);
				endTime = HexUtils.toHexL(DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8 + "", 8);
				freezeTime = StringUtils.leftPad(startTime, 8, "F");
			} else if (type == 1) {// 停止催缴
				startTime = StringUtils.leftPad(startTime, 8, "F");
				endTime = StringUtils.leftPad(startTime, 8, "F");
				freezeTime = StringUtils.leftPad(startTime, 8, "F");
			} else if (type == 2) {// 冻结
				startTime = DateUtil.getStamp(25);
				endTime = DateUtil.getStamp(15);
				freezeTime = StringUtils.leftPad(startTime, 8, "0");
			} else if (type == 3) {// 解冻
				startTime = StringUtils.leftPad(startTime, 8, "F");
				endTime = StringUtils.leftPad(startTime, 8, "F");
				freezeTime = StringUtils.leftPad(startTime, 8, "F");
			}

			String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
			String stampSend = HexUtils.toHexL(stamp + "", 8);
			String data = MessageConversionAdapter.getSetTimeData(ClientOpenTypeEnum.SETTIME.getData(),
					ClientOpenTypeEnum.SETTIME.getType(), startTime, endTime, freezeTime);
			String sendData = MessageConversionAdapter.getSendSettingTimeData(deviceId, data.length() / 2, data,
					ClientDataEnum.GENERAL.getData(), random, stampSend, getLockType(bleName));
			return sendData;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取设备ID
	 * 
	 * @param mac
	 * @return
	 */
	public static String getDeviceId(String mac) {
		mac = mac.replace(":", "");
		return mac.substring(4, mac.length());
	}

	/**
	 * 获取锁类型
	 * 
	 * @param bleName
	 * @return
	 */
	private static String getLockType(String bleName) {
		bleName = bleName.toUpperCase();
		if (bleName.contains("WSL_HC_")) {
			return "08";
		} else if (bleName.contains("WSL_HS_")) {
			return "06";
		} else if (bleName.contains("WSL_LR_")) {
			return "07";
		} else if (bleName.contains("WSL_C") || bleName.contains("WSL_D")) {
			return "03";
		}
		return "08";
	}
	/**
	 * 发送删除开门方式
	 * 
	 * @param mac
	 * @param passwordId
	 * @param type       1:指纹 2:密码 3:卡片 4:人脸 
	 * @return
	 */
	public static String getUpdateHexData(String mac, Integer passwordId, Integer type, String bleName, long stamp,String startTime,String endTime) {
		String sendMsg = "";
		String sendData = "";
		String deviceId = getDeviceId(mac);
		try {

			if (type == 1) {
				String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
				String stampSend = HexUtils.toHexL(stamp + "", 8);
				sendMsg = MessageConversionAdapter.getUpdateData(passwordId,startTime, endTime);
				sendData = MessageConversionAdapter.getSendUpdateData(deviceId, sendMsg.length() / 2, sendMsg,
						ClientDataEnum.GENERAL.getData(), ClientOpenTypeEnum.FINGERVALIDTIME.getData(), random, stampSend,
						getLockType(bleName));
				return sendData;

			} else if (type == 2) {
				String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
				String stampSend = HexUtils.toHexL(stamp + "", 8);
				sendMsg = sendMsg + MessageConversionAdapter.getUpdateData( passwordId,startTime, endTime);
				sendData = MessageConversionAdapter.getSendUpdateData(deviceId, sendMsg.length() / 2, sendMsg,
						ClientDataEnum.GENERAL.getData(), ClientOpenTypeEnum.PWDVALIDTIME.getData(), random, stampSend, getLockType(bleName));
				return sendData;
			} else if (type == 3) {
				String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
				String stampSend = HexUtils.toHexL(stamp + "", 8);
				sendMsg = sendMsg + MessageConversionAdapter.getUpdateData( passwordId,startTime, endTime);
				sendData = MessageConversionAdapter.getSendUpdateData(deviceId, sendMsg.length() / 2, sendMsg,
						ClientDataEnum.GENERAL.getData(), ClientOpenTypeEnum.CARDVALIDTIME.getData(), random, stampSend, getLockType(bleName));
				return sendData;
			} else if (type == 4) {
				String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
				String stampSend = HexUtils.toHexL(stamp + "", 8);
				sendMsg = MessageConversionAdapter.getUpdateData(passwordId, startTime, endTime);
				sendData = MessageConversionAdapter.getSendUpdateData(deviceId, sendMsg.length() / 2, sendMsg,
						ClientDataEnum.GENERAL.getData(), ClientOpenTypeEnum.FACEVALIDTIME.getData(), random, stampSend,
						getLockType(bleName));
				return sendData;
			}
		} catch (Exception e) {
			logger.error("MqttCallback getUpdateHexData:" + sendData);
		}
		return sendData;
	}

	/**
	 * ####################################NB门锁业务############################
	 **/
	/**
	 ** NB
	 *
	 * 开锁
	 * 
	 * @param mac
	 */
	public static String getNbOpenHexData(String deviceId, String stamp) {
		String openLockData = "";
		try {
			String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
			String stampSend = stamp + 60 * 60 * 8 + "";
			openLockData = MessageConversionAdapter.getNbOpenLockData(random, stampSend);
		} catch (Exception e) {
			logger.error("MqttCallback sendNbOpenMessage:" + openLockData);
		}
		return openLockData;
	}

	/**
	 * @param mac        蓝牙mac地址
	 * @param startTime  密钥有效使用开始时间
	 * @param endTime    密钥有效使用结束时间
	 * @param password   密钥数据
	 * @param passwordId 密码ID(坑号)
	 * @param type       1:指纹 2:密码 3:卡片
	 */
	public static String getNbAddHexData(String mac, String startTime, String endTime, String password,
			Integer passwordId, Integer type, String deviceId, String stamp) {
		String cmdPackage = "";
		try {

			String sendData = MessageConversionAdapter.getNbSendData(password, passwordId,
					CmdDataKey.cmdDataKeyMap.get(type + ""), startTime, endTime);
			cmdPackage = MessageConversionAdapter.getNbSendCmdData(sendData, stamp, DateUtil.getStamp());
			return cmdPackage;

		} catch (Exception e) {
			e.printStackTrace();
			logger.error("MqttCallback sendNbAddOpenTypeMessage:" + cmdPackage);
		}
		return cmdPackage;
	}

	/**
	 * @param mac        蓝牙mac地址
	 * @param startTime  密钥有效使用开始时间
	 * @param endTime    密钥有效使用结束时间
	 * @param password   密钥数据
	 * @param passwordId 密码ID(坑号)
	 * @param type       1:指纹
	 */
	public static JSONObject getNbAddFingerHexData(String mac, String startTime, String endTime, byte[] password,
			Integer passwordId, Integer type, String deviceId, String stamp) {
		String package1 = "";
		String package2 = "";
		JSONObject postParam = new JSONObject();
		String result = HexUtils.bytesToHexString(password, password.length);
		try {
			if (type == 1) {
				String finger1 = result.substring(0, result.length() / 2);
				String finger2 = result.substring(result.length() / 2, result.length());
				String sendData1 = MessageConversionAdapter.getNbSendFingerData(finger1, passwordId,
						CmdDataKey.cmdDataKeyMap.get(type + ""), startTime, endTime);
				String sendData2 = MessageConversionAdapter.getNbSendFingerData(finger2, passwordId,
						CmdDataKey.cmdDataKeyMap.get(type + ""), null, null);
				package1 = MessageConversionAdapter.getNbSendCmdData(sendData1, stamp, DateUtil.getStamp());
				package2 = MessageConversionAdapter.getNbSendCmdData(sendData2, stamp, DateUtil.getStamp());
				postParam.put("result", result);
				postParam.put("stamp", stamp);
				postParam.put("package1", package1);
				postParam.put("package2", package2);
				return postParam;
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("MqttCallback sendNbAddOpenTypeMessage:" + package1 + "---$$$$---" + package2);
		}
		postParam.put("result", 0);
		postParam.put("stamp", stamp);
		postParam.put("package1", null);
		postParam.put("package2", null);
		return postParam;
	}

	/**
	 * 发送删除开门方式
	 * 
	 * @param mac
	 * @param passwordId
	 * @param type       1:指纹 2:密码 3:卡片 5:离线密码
	 * @return
	 */
	public static String getNbDelHexData(String mac, Integer passwordId, Integer type, String deviceId, String stamp) {
		String cmdPackage = "";
		try {
			String data = "00000000";
			String endTime = "";
			String startTime = "";
			String sendData = MessageConversionAdapter.getNbSendData(data, passwordId,
					CmdDelDataKey.cmdDataKeyMap.get(type + ""), startTime, endTime);

			cmdPackage = MessageConversionAdapter.getNbSendCmdData(sendData, stamp, DateUtil.getStamp());
		} catch (Exception e) {
			logger.error("MqttCallback getNbDelHexData:" + cmdPackage);
		}
		return cmdPackage;
	}

	/**
	 * 
	 * @param sysType   1:windowd 2:linux
	 * @param startTime
	 * @param endTime
	 * @param password
	 * @param stamp
	 * @return
	 */
	public static JSONObject getNbOfflinePassword(String mac, String deviceId, Integer sysType, String startTime,
			String endTime, long stamp) {
		Long password = null;
		JSONObject postParam = new JSONObject();
		if (DateUtil.getType(startTime, endTime) == 1) {
			if (sysType == 1) {
				password = OfflinePassword.fiveMinPwd(stamp, mac,
						DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8);
			} else {
				password = OfflinePasswordSo.fiveMinPwd(stamp, mac,
						DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8);
			}
		} else if (DateUtil.getType(startTime, endTime) == 2) {
			if (sysType == 1) {
				password = OfflinePassword.threeDayPwd(stamp, mac,
						DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8,
						DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8);
			} else {
				password = OfflinePasswordSo.threeDayPwd(stamp, mac,
						DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8,
						DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8);
			}
		} else if (DateUtil.getType(startTime, endTime) == 3) {
			if (sysType == 1) {
				password = OfflinePassword.ninetythreeDayPwd(stamp, mac,
						DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8,
						DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8);
			} else {
				password = OfflinePasswordSo.ninetythreeDayPwd(stamp, mac,
						DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8,
						DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8);
			}
		} else {
			if (sysType == 1) {
				password = OfflinePassword.oneyearPwd(stamp, mac, DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8,
						DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8);
			} else {
				password = OfflinePasswordSo.oneyearPwd(stamp, mac,
						DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8,
						DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8);
			}
		}
		postParam.put("deviceId", deviceId);
		postParam.put("eventType", null);
		postParam.put("stamp", stamp);
		postParam.put("msgType", ClientOpenTypeEnum.OFFLINEPWD.getData());
		postParam.put("reportTime", DateUtil.getNowTimeStr());
		postParam.put("password", password);
		postParam.put("alarmtype", null);
		postParam.put("electric", null);
		postParam.put("simNo", null);
		postParam.put("signalInfo", null);
		postParam.put("passwordId", null);
		return postParam;
	}

	/**
	 ** 同步时间戳
	 * 
	 * @param deviceId
	 */
	public static JSONObject sendNbSyncTime(String mac, String deviceId) {
		JSONObject postParam = new JSONObject();
		String sendData = "";
		long stamplong = 0;
		try {
			stamplong = DateUtil.getNowSub(3);
			String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
			String stamp = HexUtils.toHexL(stamplong + "", 8);
			String data = MessageConversionAdapter.getOfflineData(CmdDataKey.cmdDataKeyMap.get("11"), stamp);
			sendData = MessageConversionAdapter.getNbSendCmdData(data, random, stamp);
			postParam.put("data", sendData);
			postParam.put("deviceId", deviceId);
			postParam.put("stamp", stamp);
			postParam.put("result", 1);
			return postParam;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("MqttCallback sendSyncTime:" + sendData);
		}
		postParam.put("data", sendData);
		postParam.put("deviceId", deviceId);
		postParam.put("stamp", null);
		postParam.put("result", 0);
		return postParam;
	}

	/**
	 ** 同步时间戳
	 * 
	 * @param deviceId
	 */
	public static JSONObject sendNbCurrentTime() {
		JSONObject postParam = new JSONObject();
		String sendData = "";
		long stamplong = 0;
		try {
			stamplong = DateUtil.getNowDateTime() + 60 * 60 * 8L;
			String random = ByteTools.bytesToHexString(ByteTools.generateRandomBytes(2));
			String stamp = HexUtils.toHexL(stamplong + "", 8);
			String data = MessageConversionAdapter.getCurrentTimeData(CmdDataKey.cmdDataKeyMap.get("24"), stamp);
			sendData = MessageConversionAdapter.getNbSendCmdData(data, random, stamp);
			postParam.put("data", sendData);
			postParam.put("stamp", stamp);
			postParam.put("result", 1);
			return postParam;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("MqttCallback sendSyncTime:" + sendData);
		}
		postParam.put("data", sendData);
		postParam.put("stamp", null);
		postParam.put("result", 0);
		return postParam;
	}

	public static JSONObject handlerNbReportData(String hexData, String deviceId) {
		Integer status = 0;
		String message = ByteTools.bytesToString(Base64Util.decode(hexData));
		JSONObject postParam = new JSONObject();
		String data = MessageMapAllParse.getData(message);
		Map<String, Object> datamap = MessageMapAllParse.parseMessage(data.length(), data);
		String serviceType = MessageMapAllParse.getServerType(message);
		String cmdType = MessageMapAllParse.getCmdType(data);
		postParam.put("deviceId", deviceId);
		postParam.put("eventType", null);
		postParam.put("syncTime", null);
		postParam.put("stamp", MessageMapAllParse.getRandom(message));
		postParam.put("password", null);
		postParam.put("alarmtype", null);// 3为密码多次试错报警，4为感应卡多次试错报警，5为指纹多次试错报警
		if (datamap.get("rep_errorOpenLock") != null) {
			postParam.put("alarmtype", datamap.get("rep_errorOpenLock").toString());
		} else {
			postParam.put("alarmtype", null);
		}
		if (datamap.get("electric") != null) {
			postParam.put("electric", datamap.get("electric").toString());
		} else {
			postParam.put("electric", null);
		}

		if (datamap.get("signalInfo") != null) {
			postParam.put("signalInfo", datamap.get("signalInfo").toString());
		} else {
			postParam.put("signalInfo", null);
		}
		if (datamap.get("pitNumReport") != null) {
			postParam.put("passwordId", Integer.parseInt(datamap.get("pitNumReport").toString()));
		} else {
			postParam.put("passwordId", null);
		}
		if (datamap.get("status") != null && datamap.get(cmdType) != null) {
			status = Integer.valueOf(datamap.get("status").toString());
			postParam.put("password", datamap.get(cmdType));
			postParam.put("result", datamap.get("status"));
		} else if (datamap.get(cmdType) != null) {
			postParam.put("result", datamap.get(cmdType));
		} else {
			postParam.put("result", null);
		}

		if (datamap.get(errorOpenLock) != null) {
			postParam.put("alarmtype", 3);
		}
		if (datamap.get("rep_openDoor") != null) {
			postParam.put("openStatus", datamap.get("rep_openDoor"));
		}
		if (datamap.get(alarm) != null) {
			postParam.put("alarmtype", 6);
		}
		if (datamap.get("rep_notLock") != null) {
			postParam.put("alarmtype", 7);
		}
		if (datamap.get(syncTime) != null) {
			postParam.put("syncTime", Integer.parseInt(MessageMapAllParse.getStampTime(message), 16));
			postParam.put("stamp", MessageMapAllParse.getStampTime(message));
		}
		if (datamap.get("operatorTime") != null && !datamap.get("operatorTime").equals(0)) {
			Long time = Long.valueOf(datamap.get("operatorTime").toString(), 16);
			postParam.put("reportTime", DateUtil.dateTime(time));
		} else {
			postParam.put("reportTime", DateUtil.getNowTimeStr());
		}
		if (WebEventKey.webEventKeyMap.get(cmdType) != null) {
			String type = WebEventKey.webEventKeyMap.get(cmdType).toString();
			if (type.equals("08")) {
				postParam.put("password", HexUtils.toHexL(postParam.getString("password"), 4));
			}
			if (type.equals("F9") && status == 1) {
				type = "5";
			}
			postParam.put("msgType", type);
		}
		if (datamap.get("simNo") != null) {
			postParam.put("simNo", datamap.get("simNo").toString());
		} else {
			postParam.put("simNo", null);
		}
		if (serviceType.equals("0000")) {
			Long time = Long.valueOf(MessageMapAllParse.getStampTime(message), 16) - 60 * 60 * 8;
			postParam.put("reportTime", DateUtil.dateTime(time));
			postParam.put("sendData", sendNbCurrentTime().get("data"));
			postParam.put("msgType", "00");
		} else {
			postParam.put("sendData", null);
		}

		return postParam;
	}

	public static void main(String[] args) {
		//(String mac, Integer type, Integer sysType, String startTime,String endTime, long stamp)
		JSONObject json = handlerReportData("680308a88fd260b589000168ea84db000e570c68ea70f068ed13f0ffffffff0016");
		System.out.println(json.toJSONString());
	}
}
